<?php
/**
 * Description of TableManager
 *
 * @author Magno
 */
class TableStringManager {

    private $fileString;
    private $tablesMeta;
    
    function __construct($fileString) {
        $this->fileString = $fileString;
        $this->tablesMeta = null;
    }
    
    public function getTables(){
        
        $stringTemp = $this->fileString;
        $tablesString = array();
        $index = 0;
        
         while ($index < strlen($this->fileString)) {
            
            $indIni = stripos($stringTemp, "create");
            $indFim = stripos($stringTemp, ";");
            
            if($indIni === FALSE || $indFim === FALSE)
                break;
            
            $tablesString[] = substr($stringTemp, $indIni, $indFim-$indIni);
            $stringTemp = substr($stringTemp, $indFim+1);
            $index = $indFim+2;
            
        }
        
        return $tablesString;
        
    }
    
    public function generateTableMeta($tableString){

        $tbMeta = new TableMeta($this->getTableName($tableString));

        $attString = $this->getAttributesString($tableString);

        $atStManager = new AttributeStringManager($attString);
        $tbMeta->setAttributes($atStManager->generateAllAttributeMeta());

        return $tbMeta;

    }

    public function generateAllTableMeta(){
        $tablesString = $this->getTables();
        $tablesMeta = array();

        for ($x = 0; $x < count($tablesString); $x++) {
            $tbMeta = $this->generateTableMeta($tablesString[$x]);
            $tablesMeta[$tbMeta->getName()] = $tbMeta;
        }

        for ($i = 0; $i < count($tablesString); $i++) {
            $tbMeta = $this->generateTableMeta($tablesString[$i]);
            $fkManager = new FKReferenceStringManager($this->getAttributesString($tablesString[$i]), $tablesMeta[$tbMeta->getName()], $tablesMeta);
            $tablesMeta[$tbMeta->getName()]->setFKReferences($fkManager->generateAllFKMeta());
        }

        $this->tablesMeta = $tablesMeta;

        return $tablesMeta;
    }
    
    public function getTableName($tableString){

        $stringTemp = $tableString;

        if( stripos($stringTemp, "if") === FALSE ){
            $indIni = stripos($stringTemp, "table")+strlen("table");
        }else{
            $indIni = stripos($stringTemp, "exists")+strlen("exists");
        }

        $stringTemp = substr($stringTemp, $indIni);

        $indName = Util::searchPosWordInBlankSpace($stringTemp);

        if($indName < 0) return "";

        $stringTemp = substr($stringTemp, $indName);

        if(stripos($stringTemp, ".")){
            $stringTemp = substr($stringTemp, stripos($stringTemp, ".")+1);
        }

        $indFinal = stripos($stringTemp, "(");

        $stringTemp = substr($stringTemp, 0, $indFinal);
        return str_replace(" ", "", $stringTemp);
                        
    }

    public function getAttributesString ($tableString){
        $stringTemp = $tableString;
        $stringReverse = strrev($stringTemp);

        $indIni = stripos($stringTemp, "(") + 1;
        $indFim = strlen($stringTemp) - stripos($stringReverse, ")") - 1;

        return ltrim(substr($stringTemp, $indIni, $indFim-$indIni));


    }

    public static function findByName($tables, $name){
        for ($i = 0; $i < count($tables); $i++) {
            $attName = strtolower(trim($tables[$i]->getName()));
            $name = strtolower(trim($name));
            if(strcmp($attName, $name) == 0) return $tables[$i];
        }        
        return null;
    }

}

?>
